#!/bin/bash

g_Sh_Pro_Path=$(pwd)
source $g_Sh_Pro_Path/ComEchoType.sh

g_Sh_Pro_Name="$1"
g_Sh_Chip_Name="$2"
g_Sh_Iot_Name="$3"
g_Sh_Strip_Tool=""

$g_Sh_Pro_Path/ComBuildFunc.sh $g_Sh_Pro_Path $g_Sh_Pro_Name $g_Sh_Chip_Name $g_Sh_Iot_Name
if [ $? -eq 0 ];then
    if [ -d "$(pwd)/IPC_Code/build" ];then
        rm $(pwd)/IPC_Code/build/* -rf
    fi
    exit
else
    g_Sh_Strip_Tool=`cat $g_Sh_Pro_Path/ToolStrip.txt`
fi

# 编译ipc程序 #
Sh_Func_Make_IPC_Program()
{
    mkdir -p $(pwd)/IPC_Code/build/$g_Sh_Chip_Name
    cd $(pwd)/IPC_Code/build/$g_Sh_Chip_Name
    rm CMakeCache.txt
    rm bin/*
    cmake -Wno-dev -DCMAKE_TOOLCHAIN_FILE=$g_Sh_Pro_Path/IPC_Code/Cmake/$g_Sh_Chip_Name.cmake \
    -DCHIP_TYPE=$g_Sh_Chip_Name -D$g_Sh_Chip_Name:BOOL=ON -DIOT_TYPE=$g_Sh_Iot_Name -D$g_Sh_Iot_Name:BOOL=ON $cmake_def ../..
    make -j4 #VERBOSE=1
    $g_Sh_Strip_Tool bin/*

    if [ -e "bin/iot_ipc" ];then
        cp bin/* $g_Sh_Pro_Path/filesystem/$g_Sh_Chip_Name/filesys/usr/sbin/
        cp bin/iot_ipc /tftp/
        cp bin/iot_ipc /var/www/html/
        return 0
    else
        return 1
    fi
}

# 生成镜像文件 #
Sh_Func_Make_Filesystem()
{
    # build filesystem #
    cd $g_Sh_Pro_Path/filesystem/$g_Sh_Chip_Name/
    ./mkfilesys.sh
}

Sh_Func_Make_IPC_Program
if [ $? -eq 0 ];then
    Sh_Func_Make_Filesystem
else
    sh_func_echo_err "no find iot_ipc"
fi
$g_Sh_Pro_Path/ComBuildFunc.sh $g_Sh_Pro_Path $g_Sh_Pro_Name $g_Sh_Chip_Name $g_Sh_Iot_Name "s"
